/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package com.zhangtai.modules.controller.admin;


import com.zhangtai.exception.RException;
import com.zhangtai.oss.AliyunCloudStorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

/**
 * 文件上传
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@Slf4j
@RequestMapping("oss")
@Api(tags = "文件上传接口")
public class OssUploadService {

	@Autowired
	private AliyunCloudStorageService aliyunCloudStorageService;


	/**
	 * 前台直接上传文件oss/upload
	 */

	@PostMapping("/upload")
	@ApiOperation(value = "文件上传")
//	@SysLog(descrption = "上传图片")
	public String upload(@RequestParam("file") MultipartFile file , @RequestParam(value = "catlog",required = false) String catlog
			,@RequestParam("name") String name) throws Exception {
		if (file==null) {
			throw new RException("上传文件不能为空");
		}
		if(catlog==null){
			catlog="document";
		}

		//上传文件
//		String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
		String suffix = "--"+name;



//		AliyunCloudStorageService aliyunCloudStorageService = new AliyunCloudStorageService();
		String url = aliyunCloudStorageService.uploadSuffix(file.getBytes(),catlog, suffix);



		return  url;
	}

	/**
	 * 上传的是合同 后台调的接口
	 * @param inputStream
	 * @param catlog
	 * @param name
	 * @return
	 */
	@PostMapping("/uploadStream")
	@ApiOperation(value = "文件上传")
	public String upload(@RequestParam("inputStream") InputStream inputStream , @RequestParam("catlog") String catlog,@RequestParam("name") String name)  {
		if (inputStream==null) {
			throw new RException("上传文件不能为空");
		}

		//上传文件
		String suffix =name;

		String url = aliyunCloudStorageService.uploadSuffix(inputStream, catlog,suffix);


		return  url;
	}



}
